/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			apppaths.cc
  \date			Jan 2013
  \author		TNick

  \brief		Contains the implementation of AppPaths class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	"apppaths.h"
#include	<qglobal.h>
#include	<QObject>
#include	<QDebug>
#include	<QFileInfo>
#include	<QMessageBox>
#include	<QCoreApplication>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */


/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

AppPaths *	AppPaths::uniq_ = NULL;


/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
AppPaths::AppPaths	(const QString & s_app_name , const QString & s_package )
	: MemTrack()
{
	Q_UNUSED( s_app_name );
	Q_UNUSED( s_package );

	QString kind;
	uniq_ = this;
	s_app_name_ = s_app_name;
	s_package_ = s_package;
	s_exe_path_ = QFileInfo(
				QCoreApplication::applicationFilePath()
				).absolutePath();

#ifndef	QT_NO_DEBUG
	kind = "Debug";

	/* find /build */
	QString dest_prefix = s_exe_path_;
	int i = dest_prefix.indexOf( "/build" );
	if ( i < 0 )
	{
		QMessageBox::warning( NULL, "Debug paths",
							  QObject::tr( "Unable to compute prefix path in debug environment from:" ) + s_exe_path_,
							  QMessageBox::Ok );
		return;
	}
	i += 6; /* keep the /build prefix */
	dest_prefix.remove( i, dest_prefix.length() - i );

	s_man_path_ = dest_prefix;
	s_help_path_ = dest_prefix;
	s_lang_path_ = dest_prefix;

	s_man_path_.append( "/../docs/" ).append( s_app_name );
	s_help_path_.append( "/qt_docs" );
	s_lang_path_.append( "/lang" );

#else
#  ifdef	Q_OS_WIN
	kind = "Windows";
	s_lang_path_ = s_exe_path_;
	s_lang_path_.append( "/lang" );

	s_man_path_ = s_exe_path_;
	s_man_path_.append( "/help" );
	s_help_path_ = s_man_path_;

#  elif	defined( Q_OS_DARWIN )
	kind = "Darwin";
#    error	Unable to find paths for this OS
#  elif	defined( Q_OS_UNIX )
	kind = "Unix";

	/* find /bin and remove it */
	QString dest_prefix = s_exe_path_;
	int i = dest_prefix.indexOf( "/bin" );
	if ( i < 0 )
	{
		qWarning() << QObject::tr( "Unable to compute prefix path in Unix like environment from:" )
				   << s_exe_path_;
		return;
	}
	i++; /* keep the slash */
	dest_prefix.remove( i, dest_prefix.length() - i );

	s_man_path_ = dest_prefix;
	s_help_path_ = dest_prefix;

	s_man_path_.append( "share/man/man1" );
	s_help_path_.append( "share/doc/" ).append( s_package );
	s_lang_path_ = s_help_path_;

#  else
#    error	Unable to find paths for this OS
#endif // defined( Q_OS_UNIX )
#endif // defined( QT_NO_DEBUG )
	Q_UNUSED( kind );

	/*
	QMessageBox::information(
				NULL, "Paths for " + kind,
				"s_app_name_ = " + s_app_name_ + "\n" +
				"s_package_ = " + s_package_ + "\n" +
				"s_exe_path_ = " + s_exe_path_ + "\n" +
				"s_man_path_ = " + s_man_path_ + "\n" +
				"s_help_path_ = " + s_help_path_ + "\n" +
				"s_lang_path_ = " + s_lang_path_ + "\n",
				QMessageBox::Ok );
	*/

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
AppPaths::~AppPaths	( void )
{
	uniq_ = NULL;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool					AppPaths::init				(
		const QString & s_app_name, const QString & s_package )
{
	if ( uniq_ == NULL )
	{
		new AppPaths( s_app_name, s_package );
	}
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					AppPaths::end				( void )
{
	if ( uniq_ != NULL )
	{
		delete uniq_;
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString					AppPaths::assistantPath		( void )
{
#ifndef	QT_NO_DEBUG

#  ifdef	Q_OS_WIN
	/* assistant in same directory as our executable - distributed together */
	return s_exe_path_;
#  elif	defined( Q_OS_DARWIN )
#    error	Unable to find paths for this OS
#  elif	defined( Q_OS_UNIX )
	/* assume assistant is in the path */
	return "";
#  else
#    error	Unable to find paths for this OS
#  endif // defined( Q_OS_UNIX )

#else
	/* assume assistant is in the path */
	return "";
#endif
}
/* ========================================================================= */




/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
